package com.bkhech.bigdata.tiktok_anchor.videoinfotop10;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 实现自定义map类，在这里实现核心字段的拼装
 *
 * @author guowm
 * @date 2022/7/6
 */
public class VideoInfoTop10Map extends Mapper<LongWritable, Text, Text, LongWritable> {
    /**
     *
     * @param k1 每一行的行首偏移量
     * @param v1 每一行内容
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable k1, Text v1, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
            throws IOException, InterruptedException {
        // 读取清洗之后的每一行数据
        String line = v1.toString();
        String[] fields = line.split("\t");
        String id = fields[0];
        long length = Long.parseLong(fields[4]);

        // 组装k2 v2
        Text k2 = new Text(id);
        LongWritable v2 = new LongWritable(length);

        // 把结果写出到 hdfs
        context.write(k2,v2);

    }
}
